package com.xhorse.lockplatform.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhorse.lockplatform.comm.constant.FilePathDefine;
import com.xhorse.lockplatform.comm.util.FileTransport;
import com.xhorse.lockplatform.comm.util.GsonTransferUtil;
import com.xhorse.lockplatform.comm.util.LogUtil;
import com.xhorse.lockplatform.comm.util.ZipUtil;
import com.xhorse.lockplatform.dao.mapper.XhKeytoolRenewMapper;
import com.xhorse.lockplatform.dao.mapper.XhLanguagePackMapper;
import com.xhorse.lockplatform.model.XhBrand;
import com.xhorse.lockplatform.model.XhKeytoolRenew;
import com.xhorse.lockplatform.model.XhLanguagePack;
import com.xhorse.lockplatform.service.RenewDataImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
public class RenewDataImportServiceImpl implements RenewDataImportService {

    @Autowired
    private XhLanguagePackMapper xhLanguagePackMapper;

    @Autowired
    private XhKeytoolRenewMapper xhKeytoolRenewMapper;

    @Override
    public void RenewImport() throws IOException {
        // 2.解压
        String srcPath = FilePathDefine.ZIP_DOWNLOAD_PATH + "renew.zip";
        String descDir = FilePathDefine.PARENT_RENEW_PATH;
        ZipUtil.unZipFiles(new File(srcPath), descDir);
        int version = xhLanguagePackMapper.findMaxVersion("0", 1); // 获取当前数据库最大版本号,注：以中文作为基准选取版本号
        XhLanguagePackImport(version); // 4.导入XhLanguagePack数据
        XhKeyToolRenewImport(version); // 4.导入XhKeyToolRenew数据
        FileTransport fileTransport = new FileTransport();
        fileTransport.deleteFile(FilePathDefine.RENEW + "jsonFile");
        fileTransport.deleteFile(FilePathDefine.ZIP_DOWNLOAD_PATH + "renew.zip");
    }

    @Override
    public int findMaxVersion(String dbversion, int languageId) {
        return xhLanguagePackMapper.findMaxVersion(dbversion, languageId);
    }

    /**
     * xh_language_pack数据导入
     */
    public void XhLanguagePackImport(int version) {
        String filepath = FilePathDefine.RENEW + "jsonFile\\"
                + "languagePack.json";
        String JsonString = GsonTransferUtil.JsonfileToString(filepath).trim();
        JSONObject object = JSONObject.parseObject(JsonString);
        List<XhLanguagePack> lpList = JSONObject.parseArray(object.getString("list"), XhLanguagePack.class);
        for (XhLanguagePack xhLanguagePack : lpList) {
            XhLanguagePack temp = xhLanguagePackMapper.selectInfo(xhLanguagePack);
            xhLanguagePack.setVersion(version + 1 + "");
            if (temp == null) {
                // 若是新增数据，则版本号+1
                xhLanguagePackMapper.insertSelective(xhLanguagePack);
            } else {
                if (!temp.equals(xhLanguagePack)) {
                    // 若是更新数据数据，则版本号+1
                    xhLanguagePackMapper.updateSelective(xhLanguagePack);
                }
            }
        }
    }

    /**
     * xh_keytoll_renew数据导入
     */
    public void XhKeyToolRenewImport(int version) {
        String filepath = FilePathDefine.RENEW + "jsonFile\\" + "renew.json";
        String JsonString = GsonTransferUtil.JsonfileToString(filepath).trim();
        JSONObject object = JSONObject.parseObject(JsonString);
        List<XhKeytoolRenew> renewList = JSONObject.parseArray(object.getString("list"), XhKeytoolRenew.class);
        // 获取要更新的xhBrand信息
        Map<Integer, XhLanguagePack> xhLanguagePackHashMap = new HashMap<>();
        for (XhKeytoolRenew xhKeytoolRenew : renewList) {
            String str = xhKeytoolRenew.getKeytool_adapter().replace("/R", "")
                    .replace("/C", "");
            xhKeytoolRenew.setKeytool_adapter(str);
            XhKeytoolRenew temp = xhKeytoolRenewMapper.selectInfo(xhKeytoolRenew);
            xhKeytoolRenew.setVersion(version + 1 + "");
            boolean hasAdd = false;
            if (temp == null) {
                // 若是新增数据，则版本号+1
                xhKeytoolRenewMapper.insertSelective(xhKeytoolRenew);
                hasAdd = true;
            } else {
                if (!temp.equals(xhKeytoolRenew)) {
                    // 若是更新数据数据，则版本号+1
                    xhKeytoolRenewMapper.updateSelective(xhKeytoolRenew);
                    hasAdd = true;
                }
            }
            // 更新xh_language_pack数据版本
            if (hasAdd) {
                XhLanguagePack pack = new XhLanguagePack();
                pack.setOriginal_description_id(xhKeytoolRenew.getMake_name_id());
                XhLanguagePack tempPack = xhLanguagePackMapper.selectInfo(pack);
                int key = tempPack.getId();
                if (!xhLanguagePackHashMap.containsKey(key)) {
                    XhLanguagePack languagePack = new XhLanguagePack();
                    languagePack.setLanguage_id(tempPack.getLanguage_id());
                    languagePack.setOriginal_description_id(tempPack.getOriginal_description_id());
                    languagePack.setVersion(version + 1 + "");
                    xhLanguagePackHashMap.put(key, languagePack);
                }
            }
        }
        List<XhLanguagePack> list = new ArrayList<>();
        if (!xhLanguagePackHashMap.isEmpty()) {
            Collection<XhLanguagePack> valCollection = xhLanguagePackHashMap.values();
            list = new ArrayList<>(valCollection);
            // 更新key对应的brand版本信息
            xhLanguagePackMapper.updateBatch(list);
        }
        LogUtil.getLogger().info("XhKeyToolRenewImport update xhLanguagePack Data: {}", list.size());
    }

}
